package cc.customer.cAppNews.Impl;

import cc.customer.cAppNews.CAppNewsService;
import cc.customer.common.domain.ResponseBo;
import cc.customer.common.service.impl.BaseService;
import cc.customer.common.utils.JPush.Jpush;
import cc.customer.rabbit.dao.CAppNewsMapper;
import cc.customer.rabbit.domain.CAppNews;
import cc.customer.system.dao.DeptMapper;
import cc.customer.system.dao.UserMapper;
import cc.customer.system.domain.Dept;
import cc.customer.system.domain.MyUser;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @MethodName: $
 * @Description: TODO
 * @Param: $
 * @Return: $
 * @Author: cunfeng
 * @Date: 2020/6/12 14:38
 **/
@Service
public class CAppNewsServiceImpl extends BaseService<CAppNews> implements CAppNewsService {

    private static final Logger logger = LoggerFactory.getLogger(CAppNewsServiceImpl.class);

    @Autowired
    CAppNewsMapper cAppNewsMapper;
    @Autowired
    DeptMapper deptMapper;

    @Autowired
    UserMapper userMapper;

    /**
     * 我的消息添加
     * @param cAppNews
     * @return
     */
    @Override
    public ResponseBo addCAppNews(CAppNews cAppNews) {
        try {
            MyUser user = new MyUser();
            MyUser myUser= new MyUser();
            //接收人
            if(StringUtils.isNotBlank(cAppNews.getcConvUsers())) {
                user.setUserUuid(cAppNews.getcConvUsers());
                myUser = userMapper.selectOne(user);
                //部门
                Dept dept = new Dept();
                dept.setDeptId(myUser.getDeptId());
                Dept dept1 = deptMapper.selectOne(dept);
                cAppNews.setcDept(dept1.getDeptName());
                cAppNews.setcConvUsersName(myUser.getNickname());
            }

            MyUser user1 = new MyUser();

            if(StringUtils.isNotBlank(cAppNews.getcSenderUuid())){
                //发送人
                user1.setUserUuid(cAppNews.getcSenderUuid());
                MyUser myUser1 = userMapper.selectOne(user1);
                //部门
                Dept dep1 = new Dept();
                dep1.setDeptId(myUser1.getDeptId());
                Dept dept2 = deptMapper.selectOne(dep1);
                cAppNews.setcSenderDept(dept2.getDeptName());
            }

            cAppNews.setcId(null);
            cAppNews.setCreateTime(new Timestamp(System.currentTimeMillis()));
            int row = cAppNewsMapper.insertSelective(cAppNews);
                if(row == 1){
                      if(("2").equals(cAppNews.getcConvNtype()) || ("3").equals(cAppNews.getcConvNtype()) ||("4").equals(cAppNews.getcConvNtype()) ){
                         if(myUser.getrId() != null ){
                               Map<String, String> parm = new HashMap<String, String>();
                              //这里的id是,移动端集成极光并登陆后,极光用户的rid
                               parm.put("cId",cAppNews.getcId().toString());
                               //客户id
                                 parm.put("CusId",cAppNews.getCusid());
                                 //客户资源
                                 parm.put("CousrsType",cAppNews.getcCousrsType());
                                 //客户的uuid
                                 parm.put("uuId",cAppNews.getCustomerid());
                               parm.put("id",myUser.getrId());
                             // * 设置提示信息,内容是文章标题
                               parm.put("msg",cAppNews.getcConvCont()+"");
                               parm.put("title",cAppNews.getcConvTitle());
                               Jpush.jpushAndroid(parm);
                             logger.info("极光推送成功，用户姓名为："+myUser.getNickname() +"用户极光Rid为："+myUser.getrId()+"用户uuid为"+myUser.getUserUuid());
                         }
                      }

                  return   ResponseBo.ok("添加成功");
                }
            if(("2").equals(cAppNews.getcConvNtype()) || ("3").equals(cAppNews.getcConvNtype()) ||("4").equals(cAppNews.getcConvNtype()) ) {
                logger.error("极光推送失败，用户姓名为：" + myUser.getNickname() + "用户极光Rid为：" + myUser.getrId() + "用户uuid为" + myUser.getUserUuid());
            }
            return   ResponseBo.error("添加失败");
        } catch (Exception e) {
            e.printStackTrace();
            return  ResponseBo.error("系统错误");
        }
    }

    /**
     * 我的消息详情
     * @param cAppNews
     * @return
     */
    @Override
    public ResponseBo selectCAppNews(CAppNews cAppNews) {

        try {
            CAppNews cAppNews1 = cAppNewsMapper.selectByPrimaryKey(cAppNews.getcId());

            return ResponseBo.ok("查询成功",cAppNews1);
        } catch (Exception e) {
            e.printStackTrace();
            return  ResponseBo.error("系统错误");
        }


    }

    /**
     * 我的消息列表
     * @param cAppNews
     * @return
     */
    @Override
    public List<CAppNews> cAppNewsList(CAppNews cAppNews) {
        try {
            Example example = new Example(CAppNews.class);
            example.setOrderByClause("create_time desc");
            Example.Criteria criteria = example.createCriteria();

            if(StringUtils.isNotBlank(cAppNews.getcConvNtype())){
                criteria.andEqualTo("cConvNtype",cAppNews.getcConvNtype());
            }

            if(StringUtils.isNotBlank(cAppNews.getcConvUsers())){
                criteria.andEqualTo("cConvUsers",cAppNews.getcConvUsers());
            }
            if(StringUtils.isNotBlank(cAppNews.getType())){
                criteria.andEqualTo("type",cAppNews.getType());
            }
            List<CAppNews> list = cAppNewsMapper.selectByExample(example);
            return   list;
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<CAppNews>();
        }


    }

    /**
     * 新闻未读条数
     * @param cAppNews
     * @return
     */
    @Override
    public ResponseBo cAppNewsCount(CAppNews cAppNews) {
        try {
            Example example = new Example(CAppNews.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("type",1);
            if(StringUtils.isNotBlank(cAppNews.getcConvUsers())){
                criteria.andEqualTo("cConvUsers",cAppNews.getcConvUsers());
            }
            int row = cAppNewsMapper.selectCountByExample(example);
            return ResponseBo.ok("查询成功",row);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseBo.errors("查询失败",0);
        }

    }

    @Override
    public ResponseBo cAppNewsUpdate(CAppNews cAppNews) {
        try {
            cAppNews.setType("0");
            int row = cAppNewsMapper.updateByPrimaryKeySelective(cAppNews);
            if(row == 1){
                return ResponseBo.ok("更新成功");
            }

            return ResponseBo.error("更新失败");
        } catch (Exception e){
                 e.printStackTrace();
            return ResponseBo.error("系统错误");
        }
    }
}
